Service Start, Stop

Otázka od: Opalka Martin

10. 9. 2004 14:58

Hi all.
Obraciam sa na Vas s prosbou o pomoc. Podla verejne znamich zdrojov  
(1001 TaPD) som napisal metody na %subj%.
Problem je, ze mi nepracuju ako by mali...   Vedel by mi niekto
poradit preco?

function TfmMain.ServiceStart(AMachine: String): Boolean;
var
  LServiceManager: SC_HANDLE;
  LServiceHandle: SC_HANDLE;
  LServiceStatus: TServiceStatus;
  LStringTemp: PChar;
  LCheckPoint: DWORD;
begin
  LServiceStatus.dwCurrentState := 0;
  LServiceManager := OpenSCManager(PChar(AMachine), nil,
SC_MANAGER_CONNECT);
  if (LServiceManager > 0) then
  begin
    LServiceHandle := OpenService(LServiceManager, PChar(SERVICE_NAME),
SERVICE_QUERY_STATUS);
    if (LServiceHandle > 0) then
    begin
      LStringTemp := nil;
      if StartService(LServiceHandle, 0, LStringTemp) then
      begin
        if QueryServiceStatus(LServiceHandle, LServiceStatus) then
        begin
          while (SERVICE_RUNNING <> LServiceStatus.dwCurrentState) do
          begin
            LCheckPoint := LServiceStatus.dwCheckPoint;
            Sleep(LServiceStatus.dwWaitHint);
            if not QueryServiceStatus(LServiceHandle, LServiceStatus)
then Break;
            if not LServiceStatus.dwCheckPoint < LCheckPoint then
Break;
          end;
        end;
      end;
      CloseServiceHandle(LServiceHandle);
    end;
    CloseServiceHandle(LServiceManager);
  end;
  Result := SERVICE_RUNNING = LServiceStatus.dwCurrentState;
end;

function TfmMain.ServiceStop(AMachine: String): Boolean;
var
  LServiceManagerHandle: SC_HANDLE;
  LServiceHandle: SC_HANDLE;
  LServiceStatus: TServiceStatus;
  LCheckPoint: DWORD;
begin
  LServiceManagerHandle := OpenSCManager(PChar(AMachine), nil,
SC_MANAGER_CONNECT);
  if LServiceManagerHandle > 0 then
  begin
    LServiceHandle := OpenService(LServiceManagerHandle,
PChar(SERVICE_NAME), SERVICE_QUERY_STATUS);
    if LServiceHandle > 0 then
    begin
      if ControlService(LServiceHandle, SERVICE_CONTROL_STOP,
LServiceStatus) then
      begin
        if QueryServiceStatus(LServiceHandle, LServiceStatus) then
        begin
          while (SERVICE_STOP <> LServiceStatus.dwCurrentState) do
          begin
            LCheckPoint := LServiceStatus.dwCheckPoint;
            Sleep(LServiceStatus.dwWaitHint);
            if not QueryServiceStatus(LServiceHandle, LServiceStatus)
then Break;
            if LServiceStatus.dwCheckPoint < LCheckPoint then Break;
          end;
        end;
      end;
      CloseServiceHandle(LServiceHandle);
    end;
    CloseServiceHandle(LServiceManagerHandle);
  end;
  Result := SERVICE_STOPPED = LServiceStatus.dwCurrentState;
end;